package com.hbusa.cotroller.search;

import com.hbusa.dto.TbNovels;
import com.hbusa.service.impl.*;
import com.hbusa.service.service.impl.NovelsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/doSearch")
public class SearchServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");

        String search = req.getParameter("search");

        NovelsServiceImpl novelsService = new NovelsServiceImpl();

        ArrayList<TbNovels> byString = novelsService.findByString(search);
        int novelNum = byString.size();
        HttpSession session = req.getSession();
        session.setAttribute("novelNum", novelNum);
        session.setAttribute("novels",byString);
        session.setAttribute("searchWord",search);
        resp.sendRedirect("search.jsp");


    }
}
